#include "MultiTextureIndex.h"
#include "Types.h"
#include "Base.h"
void MultiTextureIndex::_init()
{
	m_nTextureName = 0;
	m_textureIndices = new uint[SIDE_COUNT];
	m_preCalcUV = new vector2[SIDE_COUNT];
	for(int i = 0; i < SIDE_COUNT; i++)
	{
		m_textureIndices[i] = 0;
		m_preCalcUV[i] = vector2(0,0);
	}
}
void MultiTextureIndex::_destroy()
{
	ADelete(m_preCalcUV);
	ADelete(m_textureIndices);
}
MultiTextureIndex::MultiTextureIndex(void)
{
	_init();
}
MultiTextureIndex::MultiTextureIndex(uint textureName)
{
	_init();
	m_nTextureName = textureName;
}
MultiTextureIndex::MultiTextureIndex(const TextureIndex &singleIndexObject)
{
	_init();
	m_nTextureName = singleIndexObject.GetTextureName();
	for(int i = 0; i < SIDE_COUNT; i++)
	{
		m_textureIndices[i] = singleIndexObject.GetIndex();
	}
}
void MultiTextureIndex::operator=(const TextureIndex &singleIndexObject)
{
	m_nTextureName = singleIndexObject.GetTextureName();
	for(int i = 0; i < SIDE_COUNT; i++)
	{
		m_textureIndices[i] = singleIndexObject.GetIndex();
		m_preCalcUV[i] = vector2(0,0);
	}
}
MultiTextureIndex::~MultiTextureIndex(void)
{
	_destroy();
}
void MultiTextureIndex::SetTexture(uint textureName)
{
	m_nTextureName = textureName;
}
uint &MultiTextureIndex::operator[](uint subTextureID)
{
	return m_textureIndices[subTextureID];
}
uint MultiTextureIndex::GetTextureName() const
{
	return m_nTextureName;
}
uint MultiTextureIndex::GetSideCount() const
{
	return SIDE_COUNT;
}
void MultiTextureIndex::PreCalculateUVs(const vector2 &tiling)
{
	for(uint i = 0; i < SIDE_COUNT; i++)
	{
		uint xStep = m_textureIndices[i] % (int)tiling.x;
		uint yStep = (m_textureIndices[i] - xStep) / (int)tiling.x;
		m_preCalcUV[i] = vector2(1.0f/tiling.x * xStep, 1.0f/tiling.y * yStep);
	}
}
vector2 MultiTextureIndex::GetIndexXY(uint subTextureID, uint xDivisions) const
{
	uint xStep = m_textureIndices[subTextureID] % (int)xDivisions;
	uint yStep = (m_textureIndices[subTextureID] - xStep) / (int)xDivisions;
	return vector2(xStep, yStep);
}
const vector2 &MultiTextureIndex::GetUV(uint subTextureID) const
{
	return m_preCalcUV[subTextureID];
}